/**
 * A marker for an identifier property
 * @author shahma
 */
dojo.provide("medryx.orm.Identifier");
dojo.require("medryx.orm.internal.ManagedProperty");
dojo.require("medryx.exceptions.NullIdentifier");
dojo.require("dojox.uuid");
dojo.require("dojox.uuid.generateRandomUuid");

dojo.declare("medryx.orm.Identifier", medryx.orm.internal.ManagedProperty, {
	
	DEFAULT_ID_GENERATOR: {
	   generateIdentifier: function() {
		  var seed = -1;
		  this.generateIdentifier = function() {
			 return seed--;
		  }
		  
		  return this.generateIdentifier();
	   },
	   isTransientIdentifier:function(id) {
	   	return id < 0;
	   }
	},
	
	constructor:function() {
		if (!this.identifierGenerator) {
			this.identifierGenerator = this.DEFAULT_ID_GENERATOR
		}
	},
	
	generateIdentifier:function() {
		return this.identifierGenerator.generateIdentifier();
	}, 
	
	isPersistentIdentifier:function(id) {
        return !this.identifierGenerator.isTransientIdentifier(id);
       },
	   
	/**
	 * you MAY NOT initialize an object with a NULL identifier
	 * this will throw an exception if you try to!
	 * @param {Object} instance
	 * @param {Object} propertyName
	 * @param {Object} value
	 */
	initialize:function(instance, propertyName, value) {
		if (!value) {
			value = this.generateIdentifier();
		}
        instance[propertyName] = value;
		return value;
    }
});
